﻿namespace Community.CsharpSqlite.Entity
{
    /*
    ** CAPI3REF: OS Interface File Virtual Methods Object
    **
    ** Every file opened by the [sqlite3_vfs] xOpen method populates an
    ** [sqlite3_file] object (or, more commonly, a subclass of the
    ** [sqlite3_file] object) with a pointer to an instance of this object.
    ** This object defines the methods used to perform various operations
    ** against the open file represented by the [sqlite3_file] object.
    **
    ** If the xOpen method sets the sqlite3_file.pMethods element 
    ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
    ** may be invoked even if the xOpen reported that it failed.  The
    ** only way to prevent a call to xClose following a failed xOpen
    ** is for the xOpen to set the sqlite3_file.pMethods element to NULL.
    **
    ** The flags argument to xSync may be one of [SyncTypeFlag.SQLITE_SYNC_NORMAL] or
    ** [SyncTypeFlag.SQLITE_SYNC_FULL].  The first choice is the normal fsync().
    ** The second choice is a Mac OS X style fullsync.  The [SyncTypeFlag.SQLITE_SYNC_DATAONLY]
    ** flag may be ORed in to indicate that only the data of the file
    ** and not its inode needs to be synced.
    **
    ** The integer values to xLock() and xUnlock() are one of
    ** <ul>
    ** <li> [FileLockLevel.SQLITE_LOCK_NONE],
    ** <li> [FileLockLevel.SQLITE_LOCK_SHARED],
    ** <li> [FileLockLevel.SQLITE_LOCK_RESERVED],
    ** <li> [FileLockLevel.SQLITE_LOCK_PENDING], or
    ** <li> [FileLockLevel.SQLITE_LOCK_EXCLUSIVE].
    ** </ul>
    ** xLock() increases the lock. xUnlock() decreases the lock.
    ** The xCheckReservedLock() method checks whether any database connection,
    ** either in this process or in some other process, is holding a RESERVED,
    ** PENDING, or EXCLUSIVE lock on the file.  It returns true
    ** if such a lock exists and false otherwise.
    **
    ** The xFileControl() method is a generic interface that allows custom
    ** VFS implementations to directly control an open file using the
    ** [sqlite3_file_control()] interface.  The second "op" argument is an
    ** integer opcode.  The third argument is a generic pointer intended to
    ** point to a structure that may contain arguments or space in which to
    ** write return values.  Potential uses for xFileControl() might be
    ** functions to enable blocking locks with timeouts, to change the
    ** locking strategy (for example to use dot-file locks), to inquire
    ** about the status of a lock, or to break stale locks.  The SQLite
    ** core reserves all opcodes less than 100 for its own use.
    ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
    ** Applications that define a custom xFileControl method should use opcodes
    ** greater than 100 to avoid conflicts.
    **
    ** The xSectorSize() method returns the sector size of the
    ** device that underlies the file.  The sector size is the
    ** minimum write that can be performed without disturbing
    ** other bytes in the file.  The xDeviceCharacteristics()
    ** method returns a bit vector describing behaviors of the
    ** underlying device:
    **
    ** <ul>
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC512]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC1K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC2K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC4K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC8K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC16K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC32K]
    ** <li> [Iocap.SQLITE_IOCAP_ATOMIC64K]
    ** <li> [Iocap.SQLITE_IOCAP_SAFE_APPEND]
    ** <li> [Iocap.SQLITE_IOCAP_SEQUENTIAL]
    ** </ul>
    **
    ** The Iocap.SQLITE_IOCAP_ATOMIC property means that all writes of
    ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
    ** mean that writes of blocks that are nnn bytes in size and
    ** are aligned to an address which is an integer multiple of
    ** nnn are atomic.  The Iocap.SQLITE_IOCAP_SAFE_APPEND value means
    ** that when data is appended to a file, the data is appended
    ** first then the size of the file is extended, never the other
    ** way around.  The Iocap.SQLITE_IOCAP_SEQUENTIAL property means that
    ** information is written to disk in the same order as calls
    ** to xWrite().
    **
    ** If xRead() returns ExtendedResultCode.SQLITE_IOERR_SHORT_READ it must also fill
    ** in the unread portions of the buffer with zeros.  A VFS that
    ** fails to zero-fill short reads might seem to work.  However,
    ** failure to zero-fill short reads will eventually lead to
    ** database corruption.
    */
    //typedef struct sqlite3_io_methods sqlite3_io_methods;
    //struct sqlite3_io_methods {
    //  int iVersion;
    //  int (*xClose)(sqlite3_file*);
    //  int (*xRead)(sqlite3_file*, void*, int iAmt, long iOfst);
    //  int (*xWrite)(sqlite3_file*, const void*, int iAmt, long iOfst);
    //  int (*xTruncate)(sqlite3_file*, long size);
    //  int (*xSync)(sqlite3_file*, int flags);
    //  int (*xFileSize)(sqlite3_file*, long *pSize);
    //  int (*xLock)(sqlite3_file*, int);
    //  int (*xUnlock)(sqlite3_file*, int);
    //  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
    //  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
    //  int (*xSectorSize)(sqlite3_file*);
    //  int (*xDeviceCharacteristics)(sqlite3_file*);
    //  /* Additional methods may be added in future releases */
    //};
    public class sqlite3_io_methods
    {
        public int iVersion;
        public dxCheckReservedLock xCheckReservedLock;
        public dxClose xClose;
        public dxDeviceCharacteristics xDeviceCharacteristics;
        public dxFileControl xFileControl;
        public dxFileSize xFileSize;
        public dxLock xLock;
        public dxRead xRead;
        public dxSectorSize xSectorSize;
        public dxSync xSync;
        public dxTruncate xTruncate;
        public dxUnlock xUnlock;
        public dxWrite xWrite;
        /* Additional methods may be added in future releases */

        public sqlite3_io_methods(int iVersion,
                                  dxClose xClose,
                                  dxRead xRead,
                                  dxWrite xWrite,
                                  dxTruncate xTruncate,
                                  dxSync xSync,
                                  dxFileSize xFileSize,
                                  dxLock xLock,
                                  dxUnlock xUnlock,
                                  dxCheckReservedLock xCheckReservedLock,
                                  dxFileControl xFileControl,
                                  dxSectorSize xSectorSize,
                                  dxDeviceCharacteristics xDeviceCharacteristics)
        {
            this.iVersion = iVersion;
            this.xClose = xClose;
            this.xRead = xRead;
            this.xWrite = xWrite;
            this.xTruncate = xTruncate;
            this.xSync = xSync;
            this.xFileSize = xFileSize;
            this.xLock = xLock;
            this.xUnlock = xUnlock;
            this.xCheckReservedLock = xCheckReservedLock;
            this.xFileControl = xFileControl;
            this.xSectorSize = xSectorSize;
            this.xDeviceCharacteristics = xDeviceCharacteristics;
        }
    }
}